<!--
var OriGameWidth;
var OriGameHeight;
var OriGameLeftGap=10;
var OriGameRightGap=50;
var FullScreenStats=false;
var GameAutoResize=false;
var GameAllowResize=true;

function show_flash_var(src,vars) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100%" height="100%" align="middle" id="ThePreGameSwf">');
	document.write('<param name="movie" value="' + src + '">');
	document.write('<param name="quality" value="high">');	
	document.write('<param name="flashvars" value="swffile=' + vars + '">');
	document.write('<param name="AllowScriptAccess" value="always">');
    document.write('<!--[if !IE]>-->');
	document.write('<object type="application/x-shockwave-flash" data="' + src + '"  flashvars="swffile=' + vars + '" width="100%" height="100%">');
	document.write('<param name="movie" value="' + src + '">');
	document.write('<param name="flashvars" value="swffile=' + vars + '">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="AllowScriptAccess" value="always">');
    document.write('<!--<![endif]-->');
	document.write('<a href="http://www.adobe.com/go/getflash">');
	document.write('<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player">');
	document.write('</a>');
	document.write('<!--[if !IE]>-->');
	document.write('</object>');
	document.write('<!--<![endif]-->');
	document.write('</object>');
}

function show_flash(src,GameId,isclickable,wmode,AllowScriptAccess) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100%" height="100%" align="middle" id="'+GameId+'">');
	document.write('<param name="movie" value="' + src + '">');
	document.write('<param name="quality" value="high">');
	if(wmode==1)	document.write('<param name="wmode" value="direct">');
	else	document.write('<param name="wmode" value="opaque">');
	if(isclickable==0)	document.write('<param name="allowNetworking" value="internal">');	
	if(AllowScriptAccess==1)	document.write('<param name="AllowScriptAccess" value="always">');
    document.write('<!--[if !IE]>-->');
	document.write('<object type="application/x-shockwave-flash" data="' + src + '" width="100%" height="100%">');
	document.write('<param name="movie" value="' + src + '">');
	document.write('<param name="quality" value="high">');
	if(wmode==1)	document.write('<param name="wmode" value="direct">');
	else	document.write('<param name="wmode" value="opaque">');
	if(isclickable==0)	document.write('<param name="allowNetworking" value="internal">');	
	if(AllowScriptAccess==1)	document.write('<param name="AllowScriptAccess" value="always">');
    document.write('<!--<![endif]-->');
	document.write('<!--[if !IE]>-->');
	document.write('</object>');
	document.write('<!--<![endif]-->');
	document.write('</object>');
}

function show_dcr(src) {
	document.write('<object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0" width="100%" height="100%">');
	document.write('<param name="swRemote" value="swSaveEnabled=\'true\' swVolume=\'true\' swRestart=\'true\' swPausePlay=\'true\' swFastForward=\'true\' swContextMenu=\'true\'">');
	document.write('<param name="swStretchStyle" value="meet">');
	document.write('<param name="bgColor" value="#000000">');
	document.write('<param name="src" value="' + src + '">');
	document.write('<embed src="' + src + '" width="100%" height="100%" bgColor="#000000" swRemote="swSaveEnabled=\'true\' swVolume=\'true\' swRestart=\'true\' swPausePlay=\'true\' swFastForward=\'true\' swContextMenu=\'true\'" swStretchStyle="meet" type="application/x-director" pluginspage="http://www.macromedia.com/shockwave/download/"></embed>');
	document.write('</object>');
}
function CalcGameWidthAndHeight(game_width,game_height,avail_width,avail_height,min_width,min_height){
	//console.log(game_width,game_height,game_width/game_height,avail_width,avail_height);
	game_height=parseInt(game_height*avail_width/game_width);
	game_width=avail_width;
	
	if(game_height>avail_height){
		game_width=parseInt(game_width*avail_height/game_height);
		game_height=avail_height;
	}
	if(game_width<min_width){
		game_height=parseInt(game_height*min_width/game_width);
		game_width=min_width;
	}
	if(game_height<min_height){
		game_width=parseInt(game_width*min_height/game_height);
		game_height=min_height;
	}
	//console.log(game_width,game_height,game_width/game_height);
	var dimension=new Array()
	dimension[0]=game_width;
	dimension[1]=game_height;
	return dimension;	
}

function SkipAdAndShowGame(){
	//console.log("function SkipAdAndShowGame");
	//console.log($("#GameDiv_SwfDiv").width());
	if($("#GameDiv_SwfDiv").width()>100){
		return;
	}else{
		if(TheGame_type!=0){
			var SwfDiv_HTML="<iframe src='/showgame.php?type="+TheGame_type+"&swf="+TheGame_swf+"&nameid="+TheGame_nameid+"&isclickable="+TheGame_isclickable+"&wmode_direct="+TheGame_wmode_direct+"&allowscriptaccess="+TheGame_allowscriptaccess+"' width='100%' height='100%' frameborder='no' border='0' marginwidth='0' marginheight='0' scrolling='no' allowtransparency='yes'>";
			$("#GameDiv_SwfDiv").html(SwfDiv_HTML);						
		}
		//console.log("call showgame in function SkipAdAndShowGame");
		
	
		showgame();
		$( '#fblikeiframe' ).attr( 'src', function ( i, val ) { return val; });
		if(GameAutoResize) ReplayGame();
	}
}
function showskipaddiv(){ 
	$("#SkipAdDiv").show();
}

function AdjustAdSize(){
	var screen_width=$(window).width();
	var screen_height=$(window).height();

	var RightAdDivLeftGap=40;
	if(screen_width<=1024){
		RightAdDivLeftGap=40;
	}else if(screen_width>1024 && screen_width<=1366){
		RightAdDivLeftGap=80;
	}else{
		RightAdDivLeftGap=150;
	}
	RightAdDivLeftGap=5;
	
	var avail_width=screen_width-200-160-2*RightAdDivLeftGap;	//200=160+20+20
	var avail_height=screen_height-240;
	//console.log(screen_width,screen_height,avail_width,avail_height);
	//var pregamead_demension=CalcGameWidthAndHeight(550,400,avail_width,avail_height,550,400);
	var pregamead_demension= new Array(628, 594);	//595
	$('#pregamediv_ad').width(pregamead_demension[0]);
	$('#pregamediv_ad').height(pregamead_demension[1]);
	
	$('#pregamediv').width(pregamead_demension[0]+20);	//20= pregame swf padding
	$('#pregamediv').height(pregamead_demension[1]+32);		//32= top + bottom text
		
	$('#game_middle').width($('#pregamediv').width()+321+2*RightAdDivLeftGap);	//	//200=160+20+20
	
	if($('#RightAdDiv').height()>$('#pregamediv').height()){
		$('#game_middle').height($('#RightAdDiv').height()+20);	
	}else{
		$('#game_middle').height($('#pregamediv').height());	
	}

	$('#GamesThumbDiv').css({"margin-left":RightAdDivLeftGap+"px"});
	$('#RightAdDiv').css({"margin-left":RightAdDivLeftGap+"px"});

	//console.log(screen.availWidth,screen.availHeight,$(window).width(),$(window).height());	
}

function showgame(){
	//console.log("function: showgame");
	FullScreenStats=false;
	$("#pregamediv").hide();	
	$("#GamesThumbDiv").show();
	$("#GameDiv_OpareateDiv").show();
	$("#Arrow_PreviousGame").show();
	$("#Arrow_NextGame").show();
	
	var screen_width=$(window).width();
	var screen_height=$(window).height();
	/*
	if(screen_width<=1024){
		OriGameLeftGap=5;
		OriGameRightGap=5;		
	}else if(screen_width>1024 && screen_width<=1366){
		OriGameRightGap=40;
	}else{
		OriGameRightGap=80;
	}
	*/
	OriGameRightGap=5;
	OriGameLeftGap=OriGameRightGap;

	var avail_width=screen_width-360-OriGameLeftGap-OriGameRightGap;	//380= (160+20)*2(160+padding) +40(arrow)
	//console.log(screen_width);
	var avail_height=screen_height-60;	//250; 150
	//if(avail_height<550) avail_height=screen_height-50;

	var game_demension=CalcGameWidthAndHeight(OriGameWidth,OriGameHeight,avail_width,avail_height,0.5*OriGameWidth,0.5*OriGameHeight);
	if(GameAllowResize==false){
		game_demension[0]=OriGameWidth;
		game_demension[1]=OriGameHeight;		
	}

	if(game_demension[0]>=590){
		$("#GameDiv").width(game_demension[0]+40);	//40=arrow width
		$("#GameDiv_OpareateDiv").width(game_demension[0]+20);	//40(arrow) - 20(padding width)
	}else{
		$("#GameDiv").width(590+40);
		$("#GameDiv_OpareateDiv").width(590+20);
	}
	
	$('#game_middle').width(parseInt($("#GameDiv").width())+320+parseInt(OriGameLeftGap)+parseInt(OriGameRightGap));
	
	//$('#GamesThumbDiv').css({"margin-right":OriGameLeftGap+"px"});
	$('#GamesThumbDiv').css({"margin-left":OriGameLeftGap+"px"});
	$('#RightAdDiv').css({"margin-left":OriGameRightGap+"px"});
	
		
	var css = {
		'width' : game_demension[0],
		'height' : game_demension[1],
		'padding' : 0,
		'margin' : 'auto',
		'left': 0,
		'position':'static',
		'z-index' : 0
	};
	$("#GameDiv_SwfDiv").css(css);
	$("#GameDiv_SwfDiv").show();
	
	var css = {
		'width' : game_demension[0]+40,
		'height' : game_demension[1],
		'padding-top' : 10,
		'padding-right' : 0,
		'padding-bottom' : 10,
		'padding-left' : 0,
		'margin' : 'auto',
		'left': 0,
		'position':'static',
		'z-index' : 0
	};
	$("#GameDiv_ArrowAndSwf").css(css);
	
	$("#Arrow_PreviousGame").css({"margin-top":(game_demension[1]/2-40)+"px"});
	$("#Arrow_NextGame").css({"margin-top":(game_demension[1]/2-40)+"px"});	
	
	$("#GameDiv_OpareateDiv").css({"padding":"5px 10px 5px 10px"});
	$("#GameDiv_OpareateDiv").css({"margin-bottom":"5px"});
	
	var GameDivHeight=$("#GameDiv_SwfDiv").height()+$("#GameDiv_OpareateDiv").height()+35;
	if($('#GamesThumbDiv').height()>GameDivHeight){
		$('#game_middle').height($('#GamesThumbDiv').height()+10);
	}else{
		$('#game_middle').height(GameDivHeight);	
	}
	
	$(window).trigger('scroll');	// trigger lazy load images	
	
	$('.game_topthumbs').show();
	//$(window).scrollTop(180);
	//$(window).scrollTop(230);
	$('html,body').animate({scrollTop: '217px'}, 800);
}

function EnterFullScreen(){
	//console.log("function: EnterFullScreen");
	$('html,body').animate({scrollTop: '0px',scrollLeft:'0px'}, 800);
	//var oscrollTop = document.documentElement.scrollTop || document.body.scrollTop; 
	var screen_width=$(window).width();
	var screen_height=$(window).height();
	var avail_width=screen_width-32;
	var avail_height=screen_height- 50;
	var game_demension=CalcGameWidthAndHeight(OriGameWidth,OriGameHeight,avail_width,avail_height,0.5*OriGameWidth,0.5*OriGameHeight);
	if(GameAllowResize==false){
		game_demension[0]=OriGameWidth;
		game_demension[1]=OriGameHeight;		
	}

	var Left=(screen_width-game_demension[0])/2-18; //18=10(padding)+8(close button)
	var html = '<div id="OverlayBG" style="width:100%; opacity:0.9; filter:alpha(opacity:90); height:'+ $(window).height() +'px; background-color:#000;z-index:1003; position:absolute; top:0px; left:0px;"></div>';
	var closehtml = '<div id="ExitFullScreenDiv" title="Exit Full Screen"></div>';
	var css = {
		'width' : game_demension[0],
		'height' : game_demension[1],
		'padding' : 10,
		'top' :  (screen_height-game_demension[1])/2-10,
		'left': Left,
		'position':'absolute',
		'z-index' : 1004
	};
	$("#GameDiv_SwfDiv").css(css);
	 
	$('#OverlayBG').remove();
	$('#ExitFullScreenDiv').remove();
	$('#GameDiv_SwfDiv').parent().append(html);
	$("#GameDiv_SwfDiv").append(closehtml);
	
	FullScreenStats=true;
}

function ReplayGame(){
	document.getElementById('GameDiv_SwfDiv').innerHTML=$("#GameDiv_SwfDiv").html();
	//$("#GameDiv_SwfDiv").html("<div style='width:100%;height:100%'>test</div>");		
	//$("#GameDiv_SwfDiv").html("<script>document.write('test');<\/script>");		
}

function ShowAddorDeleteFav(id){
	if(IsUserLogin!=1){
		//console.log("show add to fav");
		$('#AddorRemoveFavouriteDiv').html("<a href='#' onclick=\"AddOrRemoveFav('add'," + id + ");return false;\" title=\"Add to Favourites\"><img src='http://www.gahe.com/images/orange/addfavourite.png' alt='Add to Favourites'></a>&nbsp;");
		return;
	}
	var favs=getcookie(cookieprefix+"_favourites");
	//console.log(favs);
	//console.log(favs.indexOf("," + id + ","));
	if(favs.indexOf("," + id + ",") >= 0 ){ 
		$('#AddorRemoveFavouriteDiv').html("<a href='#' onclick=\"AddOrRemoveFav('move'," + id + ");return false;\" title=\"Remove from Favourites\"><img src='http://www.gahe.com/images/orange/removefavourite.png' alt='Remove from Favourites'></a>&nbsp;");
	}else{
		$('#AddorRemoveFavouriteDiv').html("<a href='#' onclick=\"AddOrRemoveFav('add'," + id + ");return false;\" title=\"Add to Favourites\"><img src='http://www.gahe.com/images/orange/addfavourite.png' alt='Add to Favourites'></a>&nbsp;");
	}
}

function AddOrRemoveFav(action,id){
	//console.log(type + id);	return;
	$('#AddorRemoveFavouriteDiv').html("Thank you!");	
	if(IsUserLogin!=1){
		ShowLoginModal();
		ShowAddorDeleteFav(id);	
		return;
	}		
	//console.log("http://www.gahe.com/ajax_addordeletefavourite.php?action=" + action + "&id=" + id);					  
	$.ajax({  
		url: "http://www.gahe.com/ajax_addordeletefavourite.php?action=" + action + "&id=" + id,
		success: function(msg){  
			//console.log(msg);
			if(msg == 'ok')  {  
				//$('#AddorRemoveFavouriteDiv').html("Update Successfully &nbsp;");
				//console.log(msg);
			}else if(msg == 'login'){
				ShowLoginModal();
			} else{  
				//$('#ShowLoginModal').html(msg);	
			}
			ShowAddorDeleteFav(id);	
		}
	});
}

function RateGame(id,v){
	//console.log("rate");
	var html=$('#VoteGameDiv').html();
	$('#VoteGameDiv').html("Thank you!") ;

/*
	if(IsUserLogin!=1){
		ShowLoginModal();
		$('#VoteGameDiv').html(html) ;
		return;
	}
*/
	$.ajax({  
			url: "http://www.gahe.com/ajax_vote.php?id=" + id +"&v=" + v,
			success: function(msg){  
				//console.log(msg);
				if(msg == '')  { 
					$('#VoteGameDiv').html("Thank you!"); 
					//console.log(msg);
				}else{
					$('#VoteGameDiv').html(msg);
				}
		   }
	});	//end ajax
}

function AddGamePlay(id){
	$.ajax({  
			url: "http://www.gahe.com/ajax_addgameplay.php?id=" + id,
			success: function(msg){  
				//console.log(msg);
		   }
	});	//end ajax
}

$(document).ready(function(){
	FullScreenStats=false;
	AdjustAdSize();
	
	$("#ExitFullScreenDiv").live("click", function () {  
		$('#OverlayBG').remove();
		$('#ExitFullScreenDiv').remove();
		//console.log("call showgame in function ExitFullScreenDiv click");
		showgame();
	});

   	$('#GamesThumbDiv a').click(function(){
        	ga('send', 'event', 'RelatedGames', 'Games_V4','Left_Games_click');
    });
	
	$('.game_bottom_ad a').click(function(){
        	ga('send', 'event','RelatedGames', 'Games_V4','Bottom_Games_click');
    });	
	
	$(".game_thumb").mouseover(function(){
	  	$("#game_Tags").show();
	});
	$(".game_thumb").mouseout(function(){
	  	$("#game_Tags").hide();
	});	
	$(window).scrollTop(62);

	$('#GameNameDiv').click(function(){
		$('html,body').animate({scrollTop: '217px'}, 800);
	});
});


$(window).resize(function() {
	if($("#GameDiv_SwfDiv").width()>100){
		if(FullScreenStats){
			EnterFullScreen();
		}else{
			//console.log("call showgame in function window resize");
  			showgame();
		}
	}else{
		AdjustAdSize();
	}
});


$(window).keydown(function(event) {
	if (event.which == '27' && FullScreenStats) {
		 $('#ExitFullScreenDiv').click();
	}
});
-->